﻿Imports System.Windows.Forms
Imports System.Drawing

Public Class TreeCtl

    Protected manager As LayerManager

    Protected m_DragNode As TreeNode

    Public Event LayerSelected(ByVal ln As String)



    'Public Sub New(ByRef m As LayerManager)

    '    ' 此调用是 Windows 窗体设计器所必需的。
    '    InitializeComponent()


    '    ' 在 InitializeComponent() 调用之后添加任何初始化。
    '    ' 在 InitializeComponent() 调用之后添加任何初始化。
    '    manager = m

    '    AddHandler m.LayerAdded, AddressOf OnLayerAdd
    '    AddHandler m.LayerRemoved, AddressOf OnLayerRemoved
    '    AddHandler m.LayerDescChanged, AddressOf Me.OndescChanged




    'End Sub



    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Public Sub InitCtl(ByVal m As LayerManager)

        manager = m

        AddHandler m.LayerAdded, AddressOf OnLayerAdd
        AddHandler m.LayerRemoved, AddressOf OnLayerRemoved
        AddHandler m.LayerDescChanged, AddressOf Me.OndescChanged


    End Sub



    ''' <summary>
    ''' 参数指示的是层位置的索引值
    ''' </summary>
    ''' <param name="org"></param>
    ''' <param name="target"></param>
    ''' <remarks></remarks>
    Public Sub MoveLayerPosition(ByVal org As Integer, ByVal target As Integer)

        If org = target Then
            Return
        End If

        If manager.MoveLayerPosition(org, target) Then




            Dim nOrg As TreeNode = trvPolution.Nodes.Item(org).Clone()

            If org < target Then

                trvPolution.Nodes.Insert(target + 1, nOrg)


                trvPolution.Nodes.RemoveAt(org)


            Else


                trvPolution.Nodes.Insert(target, nOrg)


                trvPolution.Nodes.RemoveAt(org + 1)

            End If


        Else

            MessageBox.Show("错误，移动层动作失败！")


        End If









    End Sub
    Public Sub SetAllowDrop(ByVal b As Boolean)
        trvPolution.AllowDrop = b
    End Sub

    Protected Sub OnLayerAdd(ByVal ln As String, ByVal desc As String)
        Dim node As TreeNode


        node = New TreeNode(desc)
        node.Name = ln
        node.Checked = True
        trvPolution.Nodes.Add(node)

    End Sub

    Protected Sub OnLayerRemoved(ByVal ln As String)

        If trvPolution.Nodes.ContainsKey(ln) Then
            trvPolution.Nodes.RemoveByKey(ln)
        End If

    End Sub

    Protected Sub OndescChanged(ByVal ln As String, ByVal ds As String)


        If trvPolution.Nodes.ContainsKey(ln) Then
            trvPolution.Nodes.Item(ln).Text = ds
        End If

    End Sub





    Private Sub trvPolution_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvPolution.AfterSelect

        RaiseEvent LayerSelected(e.Node.Name)

    End Sub

    Private Sub trvPolution_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvPolution.AfterCheck

        Dim lay As Layer
        manager.GetLayer(e.Node.Name, lay)

        If lay IsNot Nothing Then

            lay.IsShowLayer = e.Node.Checked

        End If

    End Sub

    Private Sub trvPolution_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trvPolution.DoubleClick

    End Sub

    Private Sub trvPolution_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles trvPolution.DragDrop
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode") Then

            Dim p As Point = trvPolution.PointToClient(New Point(e.X, e.Y))



            Dim dragToNode As TreeNode = trvPolution.GetNodeAt(p.X, p.Y)


            If dragToNode Is Nothing Then Return






            '在目标位置插入拖到的项

            ' Dim insertIndex As Integer = dragToNode.Index
            Dim insertIndex As Integer = dragToNode.Index

            ' Dim insertNode As TreeNode = CType(trvPolution.SelectedNode.Clone, TreeNode)
            Dim insertNode As TreeNode = CType(m_DragNode.Clone, TreeNode)



            Dim nStart As Integer = m_DragNode.Index
            Dim nEnd As Integer = dragToNode.Index






            If nStart = nEnd Then
                Return
            End If




            Dim b As Boolean = manager.MoveLayerPosition(nStart, nEnd)

            If Not b Then
                Return
            End If




            If nEnd > nStart Then

                trvPolution.Nodes.Insert(insertIndex + 1, insertNode)

                ' MessageBox.Show(insertNode.Index.ToString())
            Else
                trvPolution.Nodes.Insert(insertIndex, insertNode)

            End If




            '移除移动的节点



            m_DragNode.Remove()








        End If
    End Sub

    Private Sub trvPolution_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles trvPolution.DragEnter
        If e.Data.GetDataPresent("System.Windows.Forms.TreeNode") Then

            e.Effect = DragDropEffects.Move
        Else
            e.Effect = DragDropEffects.None

        End If
    End Sub

    Private Sub trvPolution_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles trvPolution.ItemDrag
        m_DragNode = e.Item

        If e.Button = Windows.Forms.MouseButtons.Left And m_DragNode IsNot Nothing Then

            ' m_OnDrag = True

            trvPolution.DoDragDrop(m_DragNode, DragDropEffects.Copy + DragDropEffects.Move + DragDropEffects.Link)


        End If
    End Sub


    Dim readOnlyNames As New List(Of String)
    Public Property ReadOnlyLayerNames() As List(Of String)
        Get
            Return readOnlyNames
        End Get
        Set(ByVal value As List(Of String))
            readOnlyNames = value
        End Set
    End Property

    Dim curName As String = ""

    Dim bShowContext As Boolean = False
    Public Property IsShowContexMenu() As Boolean
        Get
            Return bShowContext
        End Get
        Set(ByVal value As Boolean)
            bShowContext = value
        End Set
    End Property

    Private Sub trvPolution_NodeMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles trvPolution.NodeMouseClick
        trvPolution.SelectedNode = e.Node
    End Sub

    Private Sub trvPolution_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles trvPolution.NodeMouseDoubleClick
        If e.Button = Windows.Forms.MouseButtons.Left Then

            manager.SetZoomToLayer(e.Node.Name)

        End If
    End Sub

    Public Event AskRemoveLayer(ByVal layerName As String)
    Private Sub delLayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delLayer.Click
        RaiseEvent AskRemoveLayer(curName)
    End Sub
End Class
